<?php
class myUser extends sfBasicSecurityUser
{
  public function setUserName($user_name) {
    $this->setAttribute('username', $user_name);
  }
  
  public function setUserFullName($full_name) {
    $this->setAttribute('fullname', $full_name);
  }
  
  public function setUserId($user_id) {
    $this->setAttribute('userid', $user_id);
  }
  
  public function getUserName() {
    return $this->getAttribute('username');
  }
  
  public function getUserFullName() {
    return $this->getAttribute('fullname');
  }
  
  public function getUserId() {
    return $this->getAttribute('userid');
  }
  /**
   * @param AimUser $user
   */
  public function login($user) {
    $this->setAuthenticated(true);
    $this->setCulture('vi_VN');
    //$this->setCulture('en_US');
    $this->setUserId($user->getId());
    $this->setUserName($user->getEmail());
    $this->setUserFullName($user->getFullName());
    $this->setAttribute('permissions', null);
    $this->getPermissions();
  }
  
  public function logout() {
    $this->setAuthenticated(false);
    $this->setUserId(null);
    $this->setUserName(null);
    $this->setUserFullName(null);
    $this->setAttribute('permissions', null);
  }
  
  public function resetPermissionsCache() {
    $this->setAttribute('permissions', null);
  }
  
  public function getPermissions() {
    $permissions = $this->getAttribute('permissions');
    #$permissions = null;
    if ($permissions===null) {
      $permissions = array();
      if ($user = AimUserTable::getInstance()->find(array($this->getUserId()))) {
        $permissions[] = $user->getAimRole()->getName();
        $role = $user->getRole();
        $user_permissions = AimRolePermissionTable::getInstance()->createQuery()
          ->where('role=?', $role)
          ->execute();
          
        foreach ($user_permissions as $user_permission) {
          $permissions[] = $user_permission->getAimPermission()->getName();
        }
        /*
        $user_permissions = AimUserPermissionTable::getInstance()->createQuery()
          ->where('user=?', $user->getId())
          ->execute();
        
        foreach ($user_permissions as $user_permission) {
          $permissions[] = $user_permission->getAimPermission()->getName();
        } */
      }
      
      $permissions = join('||', $permissions);
      
      $this->setAttribute('permissions', $permissions);      
    }
     
    //var_dump($permissions);
    return $permissions;
  }

  public function hasCredential($credentials, $useAnd = true) {
    if (null === $permission = $this->getPermissions()) {
      return false;
    }

    if (!is_array($credentials)) {
      return preg_match('@'.$credentials.'@', $permission);
    }

    // now we assume that $credentials is an array
    $test = false;

    foreach ($credentials as $credential) {
      // recursively check the credential with a switched AND/OR mode
      $test = $this->hasCredential($credential, $useAnd ? false : true);

      if ($useAnd) {
        $test = $test ? false : true;
      }

      if ($test) // either passed one in OR mode or failed one in AND mode
      {
        break; // the matter is settled
      }
    }

    if ($useAnd) // in AND mode we succeed if $test is false
    {
      $test = $test ? false : true;
    }

    return $test;
  }
  
  public function getCulture() {
    return 'vi';
  }
}
